# -*- coding: utf-8 -*-
#    OriginDB
#    Copyright (C) 2010  Jason Power <power.jg@gmail.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import sys
sys.path.append('../')

from PyQt4.QtGui import *
from PyQt4 import QtCore

from filter import FilterWidget
from resultsScene import ResultsScene

from aeDatabase import searchReactions

from tools import AddReactionVisual


class FilterTabWidget(QTabWidget):
	def __init__(self, searchFrame, parent=None):
		QTabWidget.__init__(self, parent)

		#self.setTabsClosable(True)

		self.searchFrame = searchFrame

		self.numFilters = 0
		self.filterNames = 0
		
		self.addFilterTab()
		
		self.addTab(QWidget(), QIcon(":/images/list-add.png"), "")

		self.connect(self, QtCore.SIGNAL("currentChanged(int)"), self.tabChanged)
		#self.connect(self, QtCore.SIGNAL("tabCloseRequested(int)"), self.tabClose)


	def addFilterTab(self):
		self.filterNames += 1
		self.insertTab(self.numFilters, FilterWidget(self.searchFrame, self), "Filter %s" % (self.filterNames))
		self.numFilters += 1


	def tabChanged(self, index):
		if index == self.numFilters:
			self.addFilterTab()
			self.setCurrentIndex(index)


	def closeCurrentTab(self):
		self.tabClose(self.currentIndex())


	def tabClose(self, index):
		if self.numFilters > 1 and index != self.numFilters:
			self.removeTab(index)
			self.numFilters -= 1
			index -= 1
			if index < 0:
				index = 0
			self.setCurrentIndex(index)

	def getFilters(self):
		l = []
		for i in range(self.numFilters):
			f = self.widget(i)
			l.append(f.getFilter())
		return l

class FilterFrame(QFrame):
	def __init__(self, searchFrame, pathwayScene):
		QFrame.__init__(self)

		self.pathwayScene = pathwayScene

		self.resultsScene = ResultsScene()
		self.resultsView = QGraphicsView(self.resultsScene)

		self.filterTabs = FilterTabWidget(searchFrame)

		self.searchButton = QPushButton("Search / Apply Filter")
		self.addButton = QPushButton("Add to Pathway")

		self.filterLayout = QVBoxLayout()
		self.filterLayout.addWidget(self.filterTabs)
		self.filterLayout.addWidget(self.searchButton)
		
		self.filterFrame = QFrame()
		self.filterFrame.setLayout(self.filterLayout)

		self.resultsLayout = QVBoxLayout()
		self.resultsLayout.addWidget(self.addButton)
		self.resultsLayout.addWidget(self.resultsView)

		self.resultsFrame = QFrame()
		self.resultsFrame.setLayout(self.resultsLayout)

		self.splitter = QSplitter()
		self.splitter.addWidget(self.filterFrame)
		self.splitter.addWidget(self.resultsFrame)
		self.splitter.setSizes([300,200])

		self.layout = QVBoxLayout()
		self.layout.addWidget(self.splitter)
		
		self.setLayout(self.layout)

		self.setFrameShape(QFrame.StyledPanel)
		self.setFrameShadow(QFrame.Raised)

		self.connect(self.searchButton, QtCore.SIGNAL("clicked()"), self.search)
		self.connect(self.addButton, QtCore.SIGNAL("clicked()"), self.addReaction)


	def addReaction(self):
		reactions = self.resultsScene.getSelected()
		for rVis in reactions:
			command = AddReactionVisual(rVis, self.pathwayScene)
			self.pathwayScene.undoStack.push(command)

	def search(self):
		filters = self.filterTabs.getFilters()
		
		reactions = searchReactions(filters)

		if not reactions:
			self.msgDialog = QMessageBox(QtGui.QMessageBox.Information, "No Items Found", "The search returned zero results.\n Try making your filters more general.", QtGui.QMessageBox.Ok)
			self.msgDialog.setModal(True)
			self.msgDialog.exec_()

		self.resultsScene.addReactions(reactions)